﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//以上几句是自动生成的

using System.IO.Ports;//我加的，包含串口相关的
using System.Text.RegularExpressions;//我加的


namespace tempsensor
{
    public partial class PC_Zigbee_wenshidu : Form
    {

        private SerialPort comm = new SerialPort();//我加的，新建一个串口变量
        private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建，定义到外面。  
        bool shoukong_flag = false;//定义手动控制标志位
        byte[] SendBuf = new byte[2];//定义串口发送字符串变量
        public PC_Zigbee_wenshidu()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comboPortName.Items.AddRange(ports);
            comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;

            //初始化SerialPort对象  
            comm.NewLine = "/r/n";
            comm.RtsEnable = true;//根据实际情况吧。 
            comm.DataReceived += comm_DataReceived;

            kaijidianqi.Enabled = false;//继电器1开按钮设置为不可用
            guanjidianqi.Enabled = false;//继电器1关按钮设置为不可用
        }

        private void comboPortName_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonOpenCloseCom_Click(object sender, EventArgs e)
        {
            //根据当前串口对象，来判断操作  
            if (comm.IsOpen)
            {
                comm.Close();
            }
            else
            {
                //关闭时点击，则设置好端口，波特率后打开  
                comm.PortName = comboPortName.Text;
                comm.BaudRate = 115200;
                try
                {
                    comm.Open();

                }
                catch (Exception ex)
                {
                    //捕获到异常信息，创建一个新的comm对象，之前的不能用了。  
                    comm = new SerialPort();
                    //现实异常信息给客户。  
                    MessageBox.Show(ex.Message);
                }
            }
            //设置按钮的状态  
            buttonOpenCloseCom.Text = comm.IsOpen ? "关闭串口" : "打开串口";
        }

        void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int n = comm.BytesToRead;//先记录下来，避免某种原因，人为的原因，操作几次之间时间长，缓存不一致
            byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
            comm.Read(buf, 0, n);//读取缓冲数据
            builder.Clear();//清除字符串构造器的内容
            //因为要访问ui资源，所以需要使用invoke方式同步ui。
            this.Invoke((EventHandler)(delegate
            {
                if (buf[0] == 'y')//如果是节点1
                {
                    // 解析烟雾浓度（3个字符）
                    this.Yanwu.Text = (buf[1] - 48).ToString() + (buf[2] - 48).ToString() + (buf[3] - 48).ToString();  // 更新UI中的烟雾浓度显示

                    if (shoukong_flag == false)
                    {
                            if (buf[1] > 50)
                            {
                                SendBuf[0] = 0x31;
                                SendBuf[1] = 0x31;
                                comm.Write(SendBuf, 0, 2);
                                System.Threading.Thread.Sleep(200);
                            }

                            if (buf[1] <= 50)
                            {
                                SendBuf[0] = 0x31;
                                SendBuf[1] = 0x30;
                                comm.Write(SendBuf, 0, 2);
                                System.Threading.Thread.Sleep(200);
                            }
                    }
                }
                else if (buf[0] == 'g')//如果是节点1
                {
                    //解析光强
                    this.WenDuZhi2.Text = (buf[1] - 48).ToString() + (buf[2] - 48).ToString();

                    if (shoukong_flag == false)
                    {
                        if (buf[1] > 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x31;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }

                        if (buf[1] <= 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x30;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }
                    }
                }
                else if (buf[0] == 'r')//如果是节点1
                {
                    //DaoZeiBaoJing.BackColor = Color.Red;//就变为红色
                    if (buf[1] == '1')//如果串口接收到0，
                    {
                        DaoZeiBaoJing.BackColor = Color.GreenYellow;//则保持绿色不变
                    }
                    else//否则有人在人体红外传感器周围活动
                    {
                        DaoZeiBaoJing.BackColor = Color.Red;//就变为红色
                    }

                    if (shoukong_flag == false)
                    {
                        if (buf[1] > 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x31;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }

                        if (buf[1] <= 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x30;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }
                    }
                }
                else if (buf[0] == 'w')//如果是节点1
                {
                    // 解析温度（2个字符）
                    this.WenDuZhi.Text = (buf[1] - 48).ToString() + (buf[2] - 48).ToString();  // 更新UI中的温度显示
                    // 解析湿度（2个字符）
                    this.ShiDuZhi.Text = (buf[4] - 48).ToString() + (buf[5] - 48).ToString();  // 更新UI中的湿度显示

                    if (shoukong_flag == false)
                    {
                        if (buf[1] > 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x31;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }

                        if (buf[1] <= 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x30;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }
                    }
                }
             
            }));
        }




        private void WenDuZhi_TextChanged(object sender, EventArgs e)
        {

        }

        private void ShiDuZhi_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonOpenLight_Click_1(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[2];
            SendBuf[0] = 0x31;
            SendBuf[1] = 0x31;
            comm.Write(SendBuf, 0, 2);//向串口发送两个字符21，
            System.Threading.Thread.Sleep(100);
        }

        private void buttonCloseLight_Click_1(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[2];
            SendBuf[0] = 0x31;
            SendBuf[1] = 0x30;
            comm.Write(SendBuf, 0, 2);//向串口发送两个字符20，
            System.Threading.Thread.Sleep(100);
        }

        private void Yanwu_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void zidong_kongzhi_CheckedChanged_1(object sender, EventArgs e)
        {
            if (zidong_kongzhi.Checked)
            {
                //zidong_kongzhi.Text = "手动控制";//被选中的时候，显示动控制，说明节点的继电器有节点自己控制
                //SendBuf[0] = 0x30;
                //SendBuf[1] = 0x30;
                //SendBuf[2] = 0x30;
                //comm.Write(SendBuf, 0, 3);
                //System.Threading.Thread.Sleep(100);
                shoukong_flag = true;
                kaijidianqi.Enabled = true;//继电器开按钮设置为可用
                guanjidianqi.Enabled = true;//继电器关按钮设置为可用
            }
            else
            {
                //zidong_kongzhi.Text = "自动控制";
                //SendBuf[0] = 0x31;
                //SendBuf[1] = 0x30;
                //SendBuf[2] = 0x30;
                //comm.Write(SendBuf, 0, 1);
                //System.Threading.Thread.Sleep(100);
                shoukong_flag = false;
                kaijidianqi.Enabled = false;//继电器开按钮设置为不可用
                guanjidianqi.Enabled = false;//继电器关按钮设置为不可用

            }
        }

        private void WenDuShuju_Enter(object sender, EventArgs e)
        {

        }
    }
}
